package com.topideal.supplychain.hades.master.sql;

import org.apache.ibatis.jdbc.SQL;
import com.topideal.supplychain.hades.master.model.Carrier;

/**
 * 根据某个条件生成SQL语句。
 * 
 * @author huanghonglin
 * @date 2017年9月3日
 */
public class CarrierSqlProvider {
    private final static String TABLE_NAME = "md_carrier";

    public String getSqlBySelective(Carrier carrier) {
        // 也可能是模糊查询：like。
        SQL sql = new SQL() {
            {
                SELECT("id,code,CARRIER_LEVEL as level,name,"
                        + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
                        + "state,create_time as createTime,"
                        + "create_by as createBy,update_time as updateTime,update_by as updateBy");
                FROM(TABLE_NAME);
                if (carrier.getId() != null) {
                    WHERE("ID=#{id}");
                }
                if (carrier.getCode() != null && !"".equals(carrier.getCode())) {
                    WHERE("CODE like CONCAT(CONCAT('%', #{code}),'%')");
                }
                if (carrier.getLevel() != null) {
                    WHERE("CARRIER_LEVEL = #{level}");
                }
                if (carrier.getName() != null && !"".equals(carrier.getName())) {
                    WHERE("NAME like CONCAT(CONCAT('%', #{name}),'%')");
                }
                if (carrier.getOutCarrierCode() != null
                        && !"".equals(carrier.getOutCarrierCode())) {
                    WHERE("OUT_CARRIER_CODE like CONCAT(CONCAT('%', #{outCarrierCode,jdbcType=VARCHAR}),'%')");
                }
                if (carrier.getKd100Code() != null && !"".equals(carrier.getKd100Code())) {
                    WHERE("kd100_code like CONCAT(CONCAT('%', #{kd100Code}),'%')");
                }
                if (carrier.getKdniaoCode() != null && !"".equals(carrier.getKdniaoCode())) {
                    WHERE("kdniao_code like CONCAT(CONCAT('%', #{kdniaoCode}),'%')");
                }
                WHERE("IS_DELETED='0'");
            }
        };
        return sql.toString() + "ORDER BY ID ASC";
    }

    public String insertSelective(Carrier carrier) {
        SQL sql = new SQL() {
            {
                INSERT_INTO(TABLE_NAME);
                if (carrier.getId() != null) {
                    VALUES("ID", "#{id}");
                }
                if (carrier.getCode() != null && !"".equals(carrier.getCode())) {
                    VALUES("CODE", "#{code}");
                }
                if (carrier.getLevel() != null) {
                    VALUES("CARRIER_LEVEL", "#{level}");
                }
                if (carrier.getName() != null && !"".equals(carrier.getName())) {
                    VALUES("NAME", "#{name}");
                }
                if (carrier.getKd100Code() != null) {
                    VALUES("kd100_code", "#{kd100Code}");
                }
                if (carrier.getOutCarrierCode() != null) {
                    VALUES("OUT_CARRIER_CODE", "#{outCarrierCode}");
                }
                if (carrier.getKdniaoCode() != null) {
                    VALUES("kdniao_code", "#{kdniaoCode}");
                }
                if (carrier.getState() != null) {
                    VALUES("STATE", "#{state}");
                }
                if (carrier.getCreateBy() != null) {
                    VALUES("CREATE_BY", "#{createBy}");
                }
                if (carrier.getCreateTime() != null) {
                    VALUES("CREATE_TIME", "#{createTime}");
                }
                if (carrier.getUpdateTime() != null) {
                    VALUES("UPDATE_TIME", "#{updateTime}");
                }
                if (carrier.getUpdateBy() != null) {
                    VALUES("UPDATE_BY", "#{updateBy}");
                }
            }
        };
        return sql.toString();
    }

    public String updateSelective(Carrier carrier) {
        SQL sql = new SQL() {
            {
                UPDATE(TABLE_NAME);
                if (carrier.getCode() != null && !"".equals(carrier.getCode())) {
                    SET("CODE=#{code}");
                }
                if (carrier.getLevel() != null) {
                    SET("CARRIER_LEVEL = #{level}");
                }
                if (carrier.getName() != null && !"".equals(carrier.getName())) {
                    SET("NAME=#{name}");
                }
                if (carrier.getKd100Code() != null) {
                    SET("kd100_code=#{kd100Code}");
                }
                if (carrier.getOutCarrierCode() != null) {
                    SET("OUT_CARRIER_CODE=#{outCarrierCode}");
                }
                if (carrier.getKdniaoCode() != null) {
                    SET("kdniao_code=#{kdniaoCode}");
                }
                if (carrier.getState() != null) {
                    SET("STATE=#{state}");
                }
                if (carrier.getCreateBy() != null) {
                    SET("CREATE_BY=#{createBy}");
                }
                if (carrier.getCreateTime() != null) {
                    SET("CREATE_TIME=#{createTime}");
                }
                if (carrier.getUpdateTime() != null) {
                    SET("UPDATE_TIME=#{updateTime}");
                }
                if (carrier.getUpdateBy() != null) {
                    SET("UPDATE_BY=#{updateBy}");
                }
                WHERE("ID=#{id}");
            }
        };
        return sql.toString();
    }

}
